namespace :db do
  desc "Fill database with sample data"

  task initialize: :environment do
    make_navigations
    make_models
    make_stations
    make_administrators
  end
end

def make_navigations
  root = Navigation.create(text: '首页', leaf: false)
  navigation = root.children.create(text: '系统设置', leaf: false)
  navigation.children.create(text: '菜单管理', model: 'Navigation', url: 'navigations')
  navigation.children.create(text: '模型管理', model: 'Model',      url: 'models')
  navigation.children.create(text: '字段管理', model: 'Column',     url: 'columns')
  navigation.children.create(text: '驻地信息', model: 'Station',    url: 'stations')
end

def make_models
  model = Model.create(name: 'Navigation', id_property: 'id', name_field: 'text')
  model.columns.create(name: 'text',        text: '名称', genre: 'string')
  model.columns.create(name: 'parent_id',   text: '父节点', genre: 'int')
  model.columns.create(name: 'leaf',        text: '是叶子', genre: 'boolean')
  model.columns.create(name: 'icon_cls',    text: '缩略图', genre: 'string')
  model.columns.create(name: 'glyph',       text: '图标', genre: 'string')
  model.columns.create(name: 'model',       text: '模型', genre: 'string')
  model.columns.create(name: 'url',         text: '链接', genre: 'string')
  model.columns.create(name: 'module',      text: '模板', genre: 'enum(panel,simplegrid,simpleline)')


  model = Model.create(name: 'Model', id_property: 'id', name_field: 'name')
  model.columns.create(name: 'name',        text: '名称', genre: 'string')
  model.columns.create(name: 'id_property', text: '主键', genre: 'string')
  model.columns.create(name: 'name_field',  text: '标志', genre: 'string')

  model = Model.create(name: 'Column', id_property: 'id', name_field: 'name')
  model.columns.create(name: 'name',        text: '名称', genre: 'string', editable: true)
  model.columns.create(name: 'model_id',    text: '模型', genre: 'int', editable: true)
  model.columns.create(name: 'text',        text: '描述', genre: 'string', editable: true)
  model.columns.create(name: 'genre',       text: '类型', genre: 'enum(date,enum,int,currency,fileSize,hex,number,percent,plural,round,usMoney,string,boolean)', editable: true)
  model.columns.create(name: 'editable',    text: '可编辑', genre: 'boolean', editable: true)
  model.columns.create(name: 'hidden',      text: '隐藏', genre: 'boolean', editable: true)
  model.columns.create(name: 'width',       text: '宽度', genre: 'int', editable: true)

  model = Model.create(name: 'Station', id_property: 'id', name_field: 'name')
  model.columns.create(name: 'name',      text: '驻地名称', genre: 'string')
  model.columns.create(name: 'abbr',      text: '驻地简称', genre: 'string')
end

def make_stations
  Station.create(name: '广州', abbr: 'gz')
  Station.create(name: '福州', abbr: 'fz')
  Station.create(name: '上海', abbr: 'sh')
  Station.create(name: '新疆', abbr: 'xj')
  Station.create(name: '江西', abbr: 'jx')
  Station.create(name: '武汉', abbr: 'wh')
  Station.create(name: '宁夏', abbr: 'nx')
end

def make_administrators
  Administrator.create(email: 'admin@admin.com', password: '12345678', password_confirmation: '12345678')
end
